﻿using System.Linq;
using Orchard;
using Orchard.Autoroute.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Settings.Metadata.Records;
using Orchard.Core.Title.Models;
using Orchard.Data;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;
using Orchard.Indexing;
using Orchard.Localization;
using OrchardHUN.QnA.Models;

namespace OrchardHUN.QnA
{
    [OrchardFeature("OrchardHUN.QnA")]
    public class Migrations : DataMigrationImpl
    {
        private IOrchardServices _orchardServices;
        private IContentManager _contentManager;
        private readonly IRepository<ContentPartDefinitionRecord> _partDefinitionRepository;

        public Localizer T { get; set; }

        public Migrations(IOrchardServices orchardServices, IRepository<ContentPartDefinitionRecord> partDefinitionRepository)
        {
            _orchardServices = orchardServices;
            _contentManager = _orchardServices.ContentManager;
            _partDefinitionRepository = partDefinitionRepository;

            T = NullLocalizer.Instance;
        }

        public int Create()
        {
            // Currently there is better way to change the taxonomy's title independent from its TermTypeName.
            var qnaTaxonomy = _contentManager.New("Taxonomy");
            qnaTaxonomy.As<TitlePart>().Title = "QnADiscussionPool"; // Fake title so the TermTypeName will be correct
            _contentManager.Create(qnaTaxonomy);
            _contentManager.Publish(qnaTaxonomy);
            var autoroutePart = qnaTaxonomy.As<AutoroutePart>();
            //autoroutePart.Title = T("Questions & Answers").ToString(); // Setting back to real title
            //autoroutePart.Slug = "qna"; // There seems to be no way to set these properly, since slug/path and term type are created both at publish
            //autoroutePart.Path = "qna";


            // This is the term created by Taxonomies
            ContentDefinitionManager.AlterTypeDefinition("QnADiscussionPool",
                cfg => cfg
                    .DisplayedAs(T("QnA Discussion Pool").ToString())
                    .WithPart("MenuPart")
                    .WithPart("BodyPart")
                    .WithPart(typeof(QnADiscussionPoolPart).Name)
            );
            HidePart(typeof(QnADiscussionPoolPart).Name);


            // First we need to create a part to attach the TaxonomyField to
            ContentDefinitionManager.AlterPartDefinition("QnADiscussionFieldsPart",
                part => part
                    .WithField(T("Categories").ToString(), field =>
                    {
                        field.OfType("TaxonomyField");
                        field.WithSetting("TaxonomyFieldSettings.Taxonomy", T("Questions & Answers").ToString());
                    })
            );
            HidePart("QnADiscussionFieldsPart");

            
            ContentDefinitionManager.AlterTypeDefinition("QnADiscussion",
                cfg => cfg
                    .DisplayedAs(T("QnA Discussion").ToString())
                    .WithPart("CommonPart")
                    .WithPart("AutoroutePart")
                    .WithPart("CommentsPart")
                    .WithPart("QnADiscussionFieldsPart")
                    .WithPart(typeof(QnADiscussionPart).Name)
                    .Indexed()
            );
            HidePart(typeof(QnADiscussionPart).Name);


            return 1;
        }

        //public int UpdateFrom1()
        //{

        //    return 2;
        //}

        private void HidePart(string name)
        {
            var partDefinition = _partDefinitionRepository.Fetch(p => p.Name == name).FirstOrDefault();
            partDefinition.Hidden = true;
        }
    }
}
